package com.dcy.daily.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author duanchunyu
 * @version 1.0.0
 * @createTime 2021年06月30日 21:29:00
 */
@RestController
public class DemoController {

    private Logger logger = LoggerFactory.getLogger(DemoController.class);

    /**
     * 查看线程堆栈信息，JVM中P147页
     */
    @RequestMapping("/stackTrace")
    public Map<String, List<StackTraceElement>> getStackTraceElement(){
        HashMap<String, List<StackTraceElement>> retMap = new HashMap<>();
        for (Map.Entry<Thread, StackTraceElement[]> threadEntry : Thread.getAllStackTraces().entrySet()) {
            Thread thread = threadEntry.getKey();
            StackTraceElement[] stackTraceElements = threadEntry.getValue();
            List<StackTraceElement> list = new ArrayList<>();
            for (StackTraceElement stackTraceElement : stackTraceElements) {
                list.add(stackTraceElement);
            }
            retMap.put(thread.getName(),list);
        }
        return retMap;
    }
}
